var ScoreEffect = require('geoScoreEffectGroupController');
var AudioManager = require('geoAudioManager');

cc.Class({
    extends: cc.Component,

    properties: {
        scoreEffectPrefab: {
            default: null,
            type: cc.Prefab,
        },
        scoreEffectContainer: {
            default: null,
            type: cc.Node
        },
        celeberateAnim: {
            default: null,
            type: cc.Animation
        },
        verygoodAnim: {
            default: null,
            type: cc.Animation,
        },
        verygoodVoice:{
            default:null,
            type: cc.AudioClip,
        },
        comeonAnim: {
            default: null,
            type: cc.Animation,
        },
        comeonVoice:{
            default:null,
            type: cc.AudioClip,
        },

    },
    onLoad() {
        // 创建得分效果器
        var effectNode = cc.instantiate(this.scoreEffectPrefab);
        this.scoreEffectContainer.addChild(effectNode);
        effectNode.setScale(1, 1);
        effectNode.position = new cc.Vec2();
        //
        this._scoreEffect = effectNode.getComponent(ScoreEffect);
    },
    setReplayButtonCallback(callback) {
        this._replayButtonCallback = callback;
    },
    onReplayButtonClick() {
        if (this._replayButtonCallback != null)
            this._replayButtonCallback();
    },
    setCloseButtonCallback(callback) {
        this._closeClickCallback = callback;
    },
    onCloseButtonClick() {
        if (this._closeClickCallback)
            this._closeClickCallback();
    },
    onDestroy() {
        this.setReplayButtonCallback(null);
        this.setCloseButtonCallback(null);
    },
    setScore(score, complete, step) {
        this._step = 0;
        this._scoreEffect.setScore(score, () => {
            if (score == 5){
                // 满分就播放太棒了
                this.verygoodAnim.node.active = true;
                this.verygoodAnim.play();
                this._audio().playVoice(this.verygoodVoice);
            }else{
                // 否则就播放加油哦
                this.comeonAnim.node.active = true;
                this.comeonAnim.play();
                this._audio().playVoice(this.comeonVoice);
            }
            //
            complete && complete();
        }, () => {
            this._step++;
            if (this._step == 3) {
                 // 达到3分时播放彩条
                this.celeberateAnim.node.active = true;
                this.celeberateAnim.play();
            }
            //
            step && step();
        });
    },
    _audio(){
        return AudioManager.Singleton;
    }
});
